package com.copm.ifm.servers.sys.service.impl.msg;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.copm.ifm.base.api.pojo.BasePO;
import com.copm.ifm.base.api.pojo.PageDTO;
import com.copm.ifm.base.api.pojo.PageVO;
import com.copm.ifm.base.basic.ServiceResponse;
import com.copm.ifm.base.basic.constant.DbConstants;
import com.copm.ifm.base.basic.enums.ResponseCodeEnum;
import com.copm.ifm.base.basic.util.AuthUtils;
import com.copm.ifm.base.service.mp.page.PageProxy;
import com.copm.ifm.servers.sys.dto.SysMsgTemplatePageDTO;
import com.copm.ifm.servers.sys.entity.SysMsgTemplate;
import com.copm.ifm.servers.sys.mapper.msg.SysMsgTemplateMapper;
import com.copm.ifm.servers.sys.service.msg.SysMsgTemplateService;
import com.copm.ifm.servers.sys.vo.SysMsgTemplatePageVO;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 消息模板（sys_msg_template） 服务实现类
 *
 * @author msp
 * @since 2021-01-04
 */
@Service
public class SysMsgTemplateServiceImpl extends ServiceImpl<SysMsgTemplateMapper, SysMsgTemplate> implements SysMsgTemplateService {

    @Override
    public PageVO<SysMsgTemplatePageVO> page(PageDTO<SysMsgTemplatePageDTO> pageDTO) {
        PageVO<SysMsgTemplatePageVO> proxy = PageProxy.proxy(baseMapper::selectForPage, pageDTO);
        List<SysMsgTemplatePageVO> records = proxy.getRecords();
        ServiceResponse<List<SysMsgTemplatePageVO>> success = new ServiceResponse<>(records, ResponseCodeEnum.SUCCESS);
        proxy.setRecords(success.getData());
        return proxy;
    }

    @Override
    public Boolean checkCode(Long tenantId, String code) {
        SysMsgTemplate sysMsgTemplate = baseMapper.selectOne(new LambdaQueryWrapper<SysMsgTemplate>()
                .eq(SysMsgTemplate::getTenantId, tenantId)
                .eq(SysMsgTemplate::getCode, code)
                .eq(BasePO::getDeleted, DbConstants.DEFAULT_NO));
        return sysMsgTemplate == null;
    }

    @Override
    public void deleteById(Long id) {
        SysMsgTemplate sysMsgTemplate = new SysMsgTemplate();
        sysMsgTemplate.setId(id);
        sysMsgTemplate.setDeleted(DbConstants.DEFAULT_YES);
        baseMapper.updateById(sysMsgTemplate);
    }

    @Override
    public void updateEntityById(SysMsgTemplate entity) {
        entity.setUpdateBy(AuthUtils.getCurrentUserId());
        entity.setUpdateTime(new Date());
        updateById(entity);
    }
}